var publicJs = require("../../public.js");
var interval = null; //倒计时函数

Page({
  data: {
    code: '',
    sendcode: '',
    time: '获取验证码', //倒计时 
    currentTime: 60,
    disabledbtn: true
  },
  onLoad: function (options) {
    this.setData({
      mobile: options.mobile
    })
  },
  codeChange: function (e) {
    //验证码
    if (e.detail.value.length > 0) {
      this.setData({
        disabledbtn: false
      });
    } else {
      this.setData({
        disabledbtn: true
      });
    }
    this.data.code = e.detail.value;
  },
  getCode: function (options) {
    var that = this;
    var currentTime = that.data.currentTime;
    var mobile = that.data.mobile;
    //获取验证码
    if (!publicJs.isTelphone(this.data.mobile)) {
      wx.showModal({
        title: '错误',
        content: '请填写正确的手机号！',
        showCancel: false,
        success: function (res) { }
      })
      return false;
    }
    that.setData({
      disabled: false
    });
    publicJs.JsonPost("Identify/getEmsCode", {
      mobile: mobile,
      type: 0
    }, function (data) {
      console.log(data);
      if (data === false) {
        that.setData({
          disabled: true
        })
        return;
      }
      console.log('获取短信验证码' + JSON.stringify(data));
      that.data.sendcode = data;
      interval = setInterval(function () {
        currentTime--;
        that.setData({
          time: '重新发送(' + currentTime + 's)'
        })
        if (currentTime <= 0) {
          clearInterval(interval)
          that.setData({
            time: '重新发送',
            currentTime: 61,
            disabled: false
          })
        }
      }, 1000)
    })
  },
  getVerificationCode() {
    this.getCode();
    var that = this
    that.setData({
      disabled: true
    })
  },
  regThree: function () {
    //注册3
    console.log("跳转mobile=" + this.data.mobile + "&code=" + this.data.code);
    wx.navigateTo({ url: "regThree?mobile=" + this.data.mobile + "&code=" + this.data.code + "&sendcode=" + this.data.sendcode })
  }
})